﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Media;

namespace Algoritmos_de_Ordenação.Algoritmos
{
    public class BubbleSort : DrawingRegion
    {
        public BubbleSort()
        {
        }
        public BubbleSort(TabItem tabItem)
            : base(tabItem, TabType.Bubble)
        {
        }

        protected override void DoSort()
        {
            
            int i;
            int j;
            int tmp;
            int comparisons = 0;
            int swaps = 0;
            
            sorted = false;

            
            for (i = numElemArray - 1; i >= 0; i--)
            {
                updateGBThread(2);
                for (j = 1; j <= i; j++)
                {
                    wf();
                    {
                        comparisons++;
                        compareTwoThread(j - 1, j);
                        updateGBThread(6);
                        updateInformationsThread(SortStatus.Sorting, comparisons, swaps);
                        
                        if (numberArray[j - 1] > numberArray[j])
                        {
                            swaps++;

                            wf();
                            updateGBThread(8);
                            swapTwoThread(j - 1, j);
                            
                            tmp = numberArray[j - 1];
                            numberArray[j - 1] = numberArray[j];
                            numberArray[j] = tmp;
                            updateInformationsThread(SortStatus.Sorting, comparisons, swaps);

                        }
                    }
                }
            }            
            endSorting(comparisons, swaps);
        }
    }
}
